

alter procedure [dbo].[p_cashdaybook_sumbalance] 
(
  @accid integer
)  
as 
begin
  set nocount on
    
  declare @standbala decimal(18,2);
  declare @bankid integer ;
  declare @startdate datetime ;
  declare @startflag integer;
  declare @booksum decimal(18,2);
  declare @balance decimal(18,2);
 
  select @bankid=bankid from td_account where accid=@accid;
  
  if (@bankid is not null) 
  begin
    select @startdate=startdate,@startflag=startflag from td_bank where bankid = @bankid;
    if (@startflag > 0 ) 
    begin
      select @standbala=standardbala from tft_mscash 
        where accountid = @accid and isinit = 1;
      select @balance=cast((sum(round(debitamt,2))-sum(round(creditamt,2))) as numeric(18,2)) 
        from tft_cashdaybook
        where initflag =0 and accountid = @accid and bookdate < @startdate;
      set @booksum = @standbala + @balance;
      update td_bank set camount = @booksum where bankid = @bankid; 
    end
  end 
end


